//素材库
'use strict';
import React,{Component} from 'react';
import styles from '../style/styles';
import Util from '../common/util';
import * as URLconstant from '../constant/URLconstant';
import Toast from '../common/ToastUtil';
import MyStroage from '../common/myStroage';
import Login from '../userCenter/login';
import {
    View,
    Text,
    TouchableOpacity,
    ScrollView,
    Image,
    ActivityIndicator
} from 'react-native';
export default class goodsSource extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            datas:null,
            showSource:0
        };
          this.successgetShare = this.successgetShare.bind(this)
          this.officialSource = this.officialSource.bind(this)
          this.sourceImg = this.sourceImg.bind(this)
          this.getMySourceSuccess = this.getMySourceSuccess.bind(this)
          this.showOfficalSource = this.showOfficalSource.bind(this)
      }

    componentWillReceiveProps(nextProps) {
        // let datas = nextProps.datas;
        // this.setState({
        //     datas:datas
        // })
    }

    componentDidMount() {
        this.setState({
            datas:this.props.datas.material_list
        })
    }

    nineShare(imgList,title) {
        this.setState({
            getShareing: true,
        })
        // let url = URLconstant.NINESHARE_URL;
        // let gid = [];
        // let datas = this.props.goods;
        // let goods = datas.goods;
        // let goods_id = goods.goods_id;
        // LOG("goods_id====="+goods_id)
        // gid['gid'] = goods_id
        // Util.get(url, gid, this.successgetShare, this.errorCallBack)
        LOG('imglist===='+JSON.stringify((imgList)))
        let shareImgList = [];
        for(let item of imgList){
            shareImgList.push(item.image_url)
        }
        let datas=[];
        datas['imgs']=shareImgList;
        datas['cmd']=1;
        datas['prefix']='wxfx9gt_';
        datas['desc']=title
        this.successgetShare(datas)

    }


    errorCallBack(){

    }

    successgetShare(datas) {
            this.setState({
                getShareing: false
            })
            LOG('分享请求' + JSON.stringify(datas))
            let cmd = datas.cmd.toString();
            let prefix = datas.prefix.toString();
            let desc = datas.desc;
            let imgs = datas.imgs;

            if (imgs == null || desc == null || prefix == null || cmd == null) {
                Toast.show('该商品数据不全,无法分享')
            } else {
                Util.nineShare(cmd, prefix, desc, imgs)
            }
    }

    //官方素材
    officialSource(){
        let that = this;
        let datas = that.state.datas;
        LOG('datas===='+JSON.stringify(datas))
        let rows;
        let list=[];
        for(let item of datas){
            rows=(
                <View style={styles.source_wrap} key={item.material_id}>
                    <Text style={styles.source_txt3}>{item.title}</Text>
                    {this.sourceImg(item.image_list)}
                    <View style={styles.source_share_wrap}>
                        <TouchableOpacity style={styles.source_share_btn} onPress={()=>this.nineShare(item.image_list,item.title)}>
                            <Text style={styles.source_txt4}>一键转发</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            )
            list.push(rows)
        }
        return list;
    }

    sourceImg(imgList){
        let rows;
        let list=[];
        for(let item of imgList){
            rows=(
                <Image
                    source={{uri:item.image_url}}
                    style={styles.goods_detail_img}
                    resizeMode={'cover'}
                    key={item.image_id}
                />
            )

            list.push(rows)
        }
        return (
            <View style={styles.img_wrap}>
                {list}
            </View>
        );
    }

    showSourceTop(){
        let that = this;
        let showSource = that.state.showSource;
        if(showSource==0){
            return(
                <View style={styles.source_top}>
                    <View style={styles.source_title_wrap}>
                        <Text style={styles.source_txt1}>官方素材</Text>
                    </View>
                    <View style={{height:40,width:1,backgroundColor:"#e3e3e3"}} />
                    <TouchableOpacity style={styles.source_title_wrap} onPress={()=>this.getMySource()}>
                        <Text style={styles.source_txt2}>我的素材</Text>
                    </TouchableOpacity>
                </View>
            )
        }else{
            return(
                <View style={styles.source_top}>
                    <TouchableOpacity style={styles.source_title_wrap} onPress={()=>this.showOfficalSource()}>
                        <Text style={styles.source_txt2}>官方素材</Text>
                    </TouchableOpacity>
                    <View style={{height:40,width:1,backgroundColor:"#e3e3e3"}} />
                    <View style={styles.source_title_wrap}>
                        <Text style={styles.source_txt1}>我的素材</Text>
                    </View>
                </View>
            )
        }
    }

    //显示官方素材
    showOfficalSource(){
        let that = this;
        that.setState({
            showSource:0,
            datas:that.props.datas.material_list
        })
    }

    //获取我的素材
    async getMySource(){
        try{
            let that = this;
            that.setState({
                showSource:1
            })
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            // LOG('KEY11111======' + JSON.stringify(userInfoDate));
            if (userInfoDate == null) {
                that.props.navigator.push(
                    {
                        component: Login,
                        passProps: {
                            navigator: this.props.navigator,
                            onLoginSuccess: this.onLoginSuccess
                        }
                    }
                );
            }else{
                let url = URLconstant.MY_MATERIAL;
                let datakey=[];
                datakey['user_id']=userInfoDate.data.user_id;
                Util.get(url,datakey,this.getMySourceSuccess,this.errorCallBack)
            }

        }catch(e){

        }

    }

    getMySourceSuccess(datas){
        LOG('datas===='+JSON.stringify(datas))
        this.setState({
            datas:datas.data
        })
    }

    render(){
        return(
            <View style={{backgroundColor:"#f0f0f0",flex:1}}>
                <View style={{position:'absolute',top:0,left:0,zIndex:9999}}>
                    {this.state.getShareing ?
                        <View
                            style={{height:Util.size.height,width:Util.size.width,justifyContent:'center',alignItems:'center',backgroundColor:'rgba(0,0,0,0.5)'}}>
                            <View
                                style={{height:80,width:150,justifyContent:'center',alignItems:'center',backgroundColor:'rgb(0,0,0)'}}>
                                <View
                                    style={{flex:1, alignItems:'center', justifyContent:'center',flexDirection:'row'}}>
                                    <ActivityIndicator color="#fff" style={{width:20,height:20,}}/>
                                    <Text style={{fontSize:16,color:'#fff'}}>加载中...</Text>
                                </View>
                            </View>
                        </View>
                        :
                        null
                    }
                </View>
                {this.showSourceTop()}
                <ScrollView>
                    {this.officialSource()}
                </ScrollView>
            </View>
        )
    }
}